<?php
defined('IN_IXDCW') or exit('Access Denied');
$menus        = array(
	array('行业圈列表', '?moduleid=' . $moduleid . '&file=' . $file),
);
$this_forward = '?moduleid=' . $moduleid . '&file=' . $file;

$options = array();
if (in_array($action, array(''))) {
	$pagesize = (isset($psize) && !empty($psize)) ? intval($psize) : 20;
}
switch ($action) {
	case 'view':
		$userid or msg(ERR_MSG);
		$item = DbIndustryInfo::find('first', array('conditions' => 'userid=' . $userid));
		$typeids = DbIndustryId::all(array('conditions' => 'userid=' . $userid));
		$arrTypeids = array();
		foreach($typeids as $type) {
			$arrTypeids[] = $type->industry;
		}
		$types = DbType::find('all', array('conditions' => array('typeid in(?)', $arrTypeids)));
		include tpl('industry_view', 'member');
		break;
	case 'delete';
		$userid or msg(ERR_MSG);
		$item    = DbIndustryInfo::find(array('conditions' => 'userid=' . $userid));
		$item->delete();
		DbIndustryId::table()->delete(array('userid'=>$userid));
		msg('删除成功', Env::referer());
	default:
		$options           = array();
		$options['select'] = "count(distinct(userid)) as num";
		$rs                = DbIndustryId::find('first', $options);
		$count             = $rs->num;

		// pagination
		$pn = new Pagination();
		$pn->recordsPerPage($pagesize);
		$pn->records($count);

		// options
		$options           = array();
		$options['order']  = 'userid DESC';
		$options['limit']  = $pagesize;
		$options['offset'] = ($pn->getPage() - 1) * $options['limit'];
		$options['select'] = "distinct(userid)";
		$result            = DbIndustryId::find('all', $options);
		$lists             = array();
		foreach ($result as $k => $industry) {
			try {
				$user      = DbMember::find($industry->userid);
				$info      = DbIndustryInfo::find('first', array('conditions' => 'userid=' . $industry->userid));
				$lists[$k] = array(
					'user' => $user,
					'info' => $info,
				);
			} catch (\ActiveRecord\RecordNotFound $e) {

			}
		}
		include tpl('industry', 'member');
		break;
}